﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using FileManager_Client.BusinessLogic;
using FileManager_Client.BusinessLogic.FileManagerUserService;
using FileManager_Client.Utility;

namespace FileManager_Client.PresentationLayer.NewUi
{
    public partial class ReNameFolderForm : BaseForm
    {
        #region Form Constructor
        private UserLoginResult userSession;
        private List<string> _hasFolderNameCollection;
        private string _folderSid;
        private string _folderOldName;
        private ToolTip _folderNameToolTip = new ToolTip();
        private FileService fileService;
        private LoadingWaitForm loadingWaitForm;
        private FileManager_Form _fileManagerForm;
        public static object lockWait = new object();
        public ReNameFolderForm(List<string> hasFolderNameCollection, string folderSid, string folderOldName, FileManager_Form fileManagerForm)
        {
            InitializeComponent();
            userSession = CacheGlobal.GetCache(GlobalString.System_Config_String.UserSession) as UserLoginResult;
            fileService = new FileService();
            Tb_FolderName.TextChanged += Tb_FolderName_TextChanged;
            Tb_FolderName.KeyPress += Tb_FolderName_KeyPress;
            _hasFolderNameCollection = hasFolderNameCollection;
            _folderSid = folderSid;
            _folderOldName = folderOldName;
            _fileManagerForm = fileManagerForm;
            Ttc_Message.ShowShadow = true;
            Ttc_Message.Rounded = true;
            Ttc_Message.ShowBeak = true;
            Ttc_Message.SetToolTipIconType(Tb_FolderName, ToolTipIconType.Information);
        }
        #endregion

        #region Txtbox FolderName Evnet
        void Tb_FolderName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13) return;
            if (CheckFolderName())
                FolderReName();
        }

        void Tb_FolderName_TextChanged(object sender, EventArgs e)
        {
            Ttc_Message.HideHint();
        }
        #endregion

        #region Button Confirm Event
        private void Btn_Confirm_Click(object sender, EventArgs e)
        {
            if (CheckFolderName())
                FolderReName();
        }
        #endregion

        #region Rename Folder Method
        private void FolderReName()
        {
            try
            {
                var backgroundWorker = new BackgroundWorker();
                backgroundWorker.DoWork += backgroundWorker_DoWork;
                var folderReNameArgs = new FolderReNameArgs
                {
                    FolderNewNamek__BackingField = Tb_FolderName.Text,
                    FolderSidk__BackingField = _folderSid,
                    UserIdk__BackingField = userSession.UserIdk__BackingField
                };
                backgroundWorker.RunWorkerAsync(folderReNameArgs);
                lock (lockWait)
                {
                    loadingWaitForm = new LoadingWaitForm();
                    loadingWaitForm.SetCaption(GlobalString.System_Config_String.MbxInfoStr);
                    loadingWaitForm.SetDescription(GlobalString.System_Config_String.FolderReNameStr);
                    loadingWaitForm.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                LogLoader.Instance.Log.Error("FolderReName has problem", ex);
                XtraMessageBox.Show("修改文件夹名失败!", GlobalString.System_Exception_String.MbxErrorStr, MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
            }
        }

        void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                var folderArgs = e.Argument as FolderReNameArgs;
                var reslut = fileService.ChangeFolderName(folderArgs);
                lock (lockWait)
                {
                    loadingWaitForm.Close();
                }
                if (reslut.Resultk__BackingField)
                {
                    XtraMessageBox.Show("修改文件夹名称成功!", GlobalString.System_Config_String.MbxInfoStr,
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Information);
                    _fileManagerForm.Invoke((EventHandler)delegate
                    {
                        _fileManagerForm.UpdateFolderName(_folderSid, Tb_FolderName.Text);
                        _fileManagerForm.UpdateListView(_folderSid);
                    });
                    Invoke((EventHandler)delegate
                    {
                        Close();
                    });
                }
                else
                {
                    var message = string.Empty;
                    switch (reslut.ResultExceptionk__BackingField)
                    {
                        case BaseExceptionEnum.NotFindFileException:
                            message = GlobalString.System_Exception_String.NotFindFolderExceptionStr;
                            break;
                        case BaseExceptionEnum.FolderReNameException:
                            message = GlobalString.System_Exception_String.FolderReNameExceptionStr;
                            break;
                    }
                    XtraMessageBox.Show(message, GlobalString.System_Exception_String.MbxErrorStr, MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                LogLoader.Instance.Log.Error("FolderReName backgroundWorker_DoWork has problem", ex);
                XtraMessageBox.Show("修改文件夹名称失败!", GlobalString.System_Exception_String.MbxErrorStr, MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                Invoke((EventHandler)delegate
                {
                    Close();
                });
            }
        }
        #endregion

        #region Check Folder Name Method
        private bool CheckFolderName()
        {
            var folderName = Tb_FolderName.Text;
            if (folderName.IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) != -1)
            {
                ShowTipMessage("文件夹名称不能包含\\/:*?\"<>| 特殊字符!");
                return false;
            }
            if (string.IsNullOrEmpty(folderName.Trim()))
            {
                ShowTipMessage("文件夹名称不能为空!");
                return false;
            }
            if (_folderOldName.Equals(folderName))
            {
                ShowTipMessage("文件夹名称没有发生变化!");
                return false;
            }
            if (_hasFolderNameCollection.Contains(folderName))
            {
                ShowTipMessage("该目录中已经包含相同名称的文件夹!");
                return false;
            }
            return true;
        }

        private void ShowTipMessage(string message)
        {
            Ttc_Message.ShowHint(message, Tb_FolderName, ToolTipLocation.BottomCenter);
        }
        #endregion

    }
}